package com.msmall.community.klg.preparer;

import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.tiles.AttributeContext;
import org.apache.tiles.context.TilesRequestContext;
import org.apache.tiles.preparer.ViewPreparer;

import com.msmall.base.cache.CacheFactory;
import com.msmall.base.util.Constants;
import com.msmall.community.common.CascadeCollection;
import com.msmall.community.common.model.Commend;
import com.msmall.community.dpt.model.Segment;
import com.msmall.community.klg.model.Article;

public class IndexEachSegmentPreparer implements ViewPreparer{
	private static final Log log = LogFactory.getLog(IndexEachSegmentPreparer.class);
	
	@SuppressWarnings("unchecked")
	public void execute(TilesRequestContext tilesContext, AttributeContext context) {
		try{
			HttpServletRequest request = (HttpServletRequest) tilesContext.getRequest();
			CascadeCollection<Segment> cs = (CascadeCollection<Segment>) CacheFactory.getInstance().getCache(Constants.CACHE_NAME_COMMON).get(Constants.CACHE_KEY_SEGMENT_CASCADECOLLECTION);
			Long segid = (Long) context.getAttribute("segid").getValue();
			
			CascadeCollection<Segment> current = cs.getAllDatas().get(segid);
			request.setAttribute("indexKlgSubSegmentCurrent", current);
			
			Map<Long,Commend> klgCommendSubSegmentImages = (Map<Long,Commend>)CacheFactory.getInstance().getCache(Constants.CACHE_NAME_COMMON).get(Constants.CACHE_KEY_KLG_COMMEND_SUBSEGMENT_IMAGES);
			
			Commend commend = null;
			if(klgCommendSubSegmentImages != null){
				commend = klgCommendSubSegmentImages.get(segid);
			}
			request.setAttribute("indexKlgSubSegmentImage", commend);
			
			Map<Long, List<Article>> klgSubSegmentHot = (Map<Long, List<Article>>)CacheFactory.getInstance().getCache(Constants.CACHE_NAME_COMMON).get(Constants.CACHE_KEY_KLG_SUBSEGMENT_HOT);
			List<Article> articles =null;
			if(klgSubSegmentHot != null){
				articles = klgSubSegmentHot.get(segid);
			}
			request.setAttribute("indexKlgSubSegmentHot", articles);
		}catch(Exception e){
			log.debug(e);
		}
	}
}